package com.cesce.batch.cobros.items;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.core.io.FileSystemResource;


import com.cesce.batch.cobros.reverse.Cobcomunicados;
import com.cesce.batch.cobros.reverse.Cobregistro;

public class MultiCsvComunicadosItemWriter implements ItemWriter<Cobcomunicados>, ItemStream {
private FlatFileItemWriter<Cobcomunicados> delegate;
	
	public void write(List<? extends Cobcomunicados> items) throws Exception {
		
		Cobcomunicados comuni = items.get(0);
		this.delegate.close();
		File f = new File(comuni.getIdregistro().getIdregistro().toString() + "_cobros_unificado2.csv");
		f.createNewFile();
		this.delegate.setResource(new FileSystemResource(f));
		this.delegate.open(new ExecutionContext());
		this.delegate.write(items);

	}

	public void setDelegate(FlatFileItemWriter<Cobcomunicados> delegate) {
		this.delegate = delegate;
	}

	public void close() throws ItemStreamException {
		this.delegate.close();
	}

	public void open(ExecutionContext executionContext) throws ItemStreamException {
		this.delegate.open(executionContext);
	}
	
	public void update(ExecutionContext executionContext) throws ItemStreamException {
		this.delegate.update(executionContext);
	}



}
